package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.core.entity.ResendRecord;
import org.xxpay.core.entity.ResendRecordExample;
import org.xxpay.core.entity.SysUser;
import org.xxpay.core.service.IResendRecordService;
import org.xxpay.service.dao.mapper.PayOrderMapper;
import org.xxpay.service.dao.mapper.ResendRecordMapper;
import org.xxpay.service.dao.mapper.SysUserMapper;

import java.util.Date;
import java.util.List;

/**
 * @author: pangxaioyu
 * @date: 2020/02/11
 * @description:
 */
@Service
public class ResendRecordServiceImpl implements IResendRecordService {

    @Autowired
    private ResendRecordMapper resendRecordMapper;

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public int add(ResendRecord resendRecord) {
        return resendRecordMapper.insertSelective(resendRecord);
    }

    @Override
    public int update(ResendRecord resendRecord) {
        return resendRecordMapper.updateByPrimaryKeySelective(resendRecord);
    }

    @Override
    public ResendRecord findById(Long id) {
        return resendRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ResendRecord> select(int offset, int limit, ResendRecord resendRecord, Date createTimeStart, Date createTimeEnd) {
        ResendRecordExample example = new ResendRecordExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        ResendRecordExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, resendRecord, createTimeStart, createTimeEnd);
        return resendRecordMapper.selectByExample(example);
    }

    @Override
    public Integer count(ResendRecord resendRecord, Date createTimeStart, Date createTimeEnd) {
        ResendRecordExample example = new ResendRecordExample();
        ResendRecordExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, resendRecord, createTimeStart, createTimeEnd);
        return resendRecordMapper.countByExample(example);
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public ResendRecord insertRecord(String payOrderId, Long operateUserId, String remark, byte operateUserBelongSystem) {
        // 1.获取订单信息
        PayOrder payOrder = payOrderMapper.selectByPrimaryKey(payOrderId);

        // 存入补单信息
        ResendRecord resendRecord = new ResendRecord();

        // 2.获取操作人信息 支持运营平台
        resendRecord.setOperateUserBelongSystem(operateUserBelongSystem); //所属系统
        if(operateUserBelongSystem == MchConstant.SYSTYPE_MGR){

            SysUser sysUser = sysUserMapper.selectByPrimaryKey(operateUserId);
            resendRecord.setOperateUserId(sysUser.getUserId());
            resendRecord.setOperateUserName(sysUser.getUserName());

        }

        resendRecord.setPayOrderId(payOrder.getPayOrderId());
        resendRecord.setAmount(payOrder.getAmount());
        resendRecord.setBeforeState(payOrder.getStatus());
        resendRecord.setAfterState(PayConstant.PAY_STATUS_SUCCESS);
        resendRecord.setRemark(remark);

        resendRecord.setResendType(PayConstant.PAY_RESEND_RECORD_TYPE_ORDER);
        resendRecord.setResendState(PayConstant.PAY_RESEND_RECORD_STATE_CREATE);
        resendRecord.setResData(null);
        resendRecordMapper.insertSelective(resendRecord);
        return resendRecord;
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public int updateState2Finish(ResendRecord resendRecord) {
        resendRecord.setResendState(PayConstant.PAY_RESEND_RECORD_STATE_FINISH);
        return resendRecordMapper.updateByPrimaryKey(resendRecord);
    }

    void setCriteria(ResendRecordExample.Criteria criteria, ResendRecord resendRecord, Date createTimeStart, Date createTimeEnd) {
        if(resendRecord != null) {
            if(resendRecord.getId() != null) criteria.andIdEqualTo(resendRecord.getId());
            if(StringUtils.isNotEmpty(resendRecord.getPayOrderId())) criteria.andPayOrderIdEqualTo(resendRecord.getPayOrderId());
            if(resendRecord.getAmount() != null) criteria.andAmountEqualTo(resendRecord.getAmount());
            if(resendRecord.getBeforeState() != null) criteria.andBeforeStateEqualTo(resendRecord.getBeforeState());
            if(resendRecord.getAfterState() != null) criteria.andAfterStateEqualTo(resendRecord.getAfterState());
            if(resendRecord.getResendState() != null) criteria.andResendStateEqualTo(resendRecord.getResendState());
            if(resendRecord.getOperateUserId() != null) criteria.andOperateUserIdEqualTo(resendRecord.getOperateUserId());
            if(resendRecord.getResendType() != null && resendRecord.getResendType() != -99) criteria.andResendTypeEqualTo(resendRecord.getResendType());
            if(StringUtils.isNotEmpty(resendRecord.getOperateUserName())) criteria.andOperateUserNameLike("%"+ resendRecord.getOperateUserName() +"%");
        }
        if(createTimeStart != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(createTimeStart);
        }
        if(createTimeEnd != null) {
            criteria.andCreateTimeLessThanOrEqualTo(createTimeEnd);
        }
    }

}
